package com.donguk.file.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.donguk.file.dto.Sample;
import com.donguk.file.service.SamapleService;
import com.donguk.file.service.SampleServiceImpl;


@Controller
public class RestController {
	private Log log = LogFactory.getLog(this.getClass());
	
	@Autowired
	private SamapleService sampleService;
	/**
	 * rest sample for get request method
	 * @return
	 */
	@RequestMapping( value="/data", method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> data(){ 
		Map<String, Object> resMap = new HashMap<String, Object>();
		log.info("get method call sample");
		resMap.put("test", "hello");
		resMap.put("test2", "hello2");
		return resMap;
	}
	
	/**
	 * rest sample for multi part method or post 
	 * @return
	 */
	@RequestMapping(value="/file", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> file(HttpServletRequest request,HttpServletResponse response) throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		String id = multipartRequest.getParameter("id");
		log.info("this is id = " + id);
		
		//MultipartFile file = multipartRequest.getFile("userfile"); -- 1st method 
		//Map<String, MultipartFile> files = multipartRequest.getFileMap(); -- or 2nd method
		Set<String> keys = multipartRequest.getFileMap().keySet(); // or 3rd method
		String fileKey = null;
		
		Iterator<String> it = keys.iterator();
		
		while (it.hasNext()) {
			fileKey = it.next().toString();
			log.info("this is file : " + fileKey);
			MultipartFile file = multipartRequest.getFile(fileKey);	// this is file 
			// you can write file 
		}
		
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("rtnmsg", "ok");
		return resMap;
	}
	
	/**
	 * rest sample for multi part method or post 
	 * @return
	 */
	@RequestMapping(value="/testdb", method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> testdb(HttpServletRequest request,HttpServletResponse response) throws IOException {
		try{
			sampleService.testdb();
		} catch(Exception e){
			log.error(e);
			e.printStackTrace();
		}
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("rtnmsg", "ok");
		return resMap;
	}
}
